import { createApp } from 'vue'
import { createPinia } from 'pinia'
import router from './router'
import App from './App.vue'
import Vant from 'vant'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import 'vant/lib/index.css'
import 'virtual:svg-icons-register'
import { loadSvg } from '@/icons'
import SvgIcon from '@/components/SvgIcon/index.vue'
import { setupErrorHandler } from '@/utils/error-handler'
// 导入Element Plus图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

const app = createApp(App)
const pinia = createPinia()

setupErrorHandler(app)

app.use(router)
app.use(pinia)
app.use(Vant)
app.use(ElementPlus)
loadSvg(app)
app.component('svg-icon', SvgIcon)

// 注册所有Element Plus图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  // 防止组件重复注册
  if (!app.component(key)) {
    app.component(key, component)
  }
}

// 添加用户活动监听，更新最后活动时间
function setupActivityMonitoring() {
  // 初始化最后活动时间
  const token = localStorage.getItem('token') || sessionStorage.getItem('token')
  if (token) {
    localStorage.setItem('lastActivity', new Date().getTime().toString())
  }

  // 监听用户活动事件
  const events = ['mousedown', 'mousemove', 'keypress', 'scroll', 'touchstart']
  const activityHandler = () => {
    const token = localStorage.getItem('token') || sessionStorage.getItem('token')
    if (token) {
      localStorage.setItem('lastActivity', new Date().getTime().toString())
    }
  }

  // 添加事件监听器
  events.forEach(event => {
    document.addEventListener(event, activityHandler, { passive: true })
  })
}

// 设置用户活动监控
setupActivityMonitoring()

app.mount('#app') 